package com.api.shopapi.mapper;

import com.api.shopapi.entity.GoodsClass;
import com.api.shopapi.entity.dto.GoodsBusinessCodeDTO;
import com.api.shopapi.entity.dto.GoodsClassDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface GoodsClassMapper {

    @Insert("insert into goods_class " +
            "(" +
            "GoodsClassCode, " +
            "ParentGoodsClassCode, " +
            "Name, " +
            "Icon, " +
            "Sort, " +
            "Status, " +
            "IsGroup" +
            ") " +
            "VALUES " +
            "(#{goodsClass.GoodsClassCode} ," +
            "#{goodsClass.ParentGoodsClassCode} ," +
            "#{goodsClass.Name} ," +
            "#{goodsClass.Icon} ," +
            "#{goodsClass.Sort} ," +
            "#{goodsClass.Status} ," +
            "#{goodsClass.IsGroup} )")
    int insert(@Param("goodsClass") GoodsClass goodsClass);


    @Update("update  goods_class set Status=2 where GoodsClassCode=#{goodsClassCode}")
    int delete(@Param("goodsClassCode")String goodsClassCode);

    @Update("update goods_class set " +
            "ParentGoodsClassCode=#{goodsClass.ParentGoodsClassCode} ," +
            "Name=#{goodsClass.Name}," +
            "Icon=#{goodsClass.Icon} ," +
            "Sort=#{goodsClass.Sort}," +
            "Status=#{goodsClass.Status}," +
            "IsGroup=#{goodsClass.IsGroup}  " +
            "where GoodsClassCode=#{goodsClass.GoodsClassCode}     ")
    int update(@Param("goodsClass")GoodsClass goodsClass);

    @Select("select * from goods_class where GoodsClassCode=#{goodsClassCode}  limit 1")
    GoodsClass getByGoodsClassCode(@Param("goodsClassCode")String goodsClassCode);

    @Select("select * from goods_class where Name=#{name} and Status=1 limit 1")
    GoodsClass getByName(@Param("name")String name);


    @Select("select * from goods_class where Status=1  order by Sort desc")
    List<GoodsClass> getAllList();


    @Select("select * from goods_class where ParentGoodsClassCode=#{parentGoodsClassCode} and Status=1 order by Sort desc")
    List<GoodsClassDTO> getListByParentGoodsClassCode(@Param("parentGoodsClassCode") String parentGoodsClassCode);

    @Select("select * from goods_class where ParentGoodsClassCode=#{parentGoodsClassCode} order by Sort desc")
    List<GoodsClassDTO> getAllListByParentGoodsClassCode(@Param("parentGoodsClassCode") String parentGoodsClassCode);

    @Select("<script>" +
            "select * from goods_class " +
            "where 1=1 "+
            "<if test ='name !=null and name!=\"\" '>" +
            "and Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='status!=-1'>" +
            "and Status=#{status} " +
            "</if>" +
            "order by Sort desc" +
            "</script>")
    List<GoodsClass> getPageList(@Param("name")String name,@Param("status") int status);


    @Select("<script>" +
            "select * from goods_class " +
            "where ParentGoodsClassCode=#{parentGoodsClassCode} "+
            "<if test ='name !=null and name!=\"\" '>" +
            "and Name like concat('%',#{name},'%') " +
            "</if>" +
            "order by Sort desc" +
            "</script>")

    List<GoodsClassDTO> getPageListByParentGoodsClassCode(@Param("parentGoodsClassCode") String parentGoodsClassCode,@Param("name")String name);


    //根据商家和菜品名称查询每个餐品对应的数量
    @Select("select COALESCE(sum(og.number),0)" +
            " from order_goods as og,order_business as ob  " +
            " where og.OrderBusinessCode = ob.OrderBusinessCode and ob.BusinessCode= #{businessCode}\n" +
            " and og.GoodsCode = #{GoodsCode} and ob.CreateTime between #{startTime} and #{endTime} " )
    int getBusinessGoodsCode(@Param("businessCode") String businessCode,
                             @Param("GoodsCode")String  GoodsCode,
                             @Param("startTime") String startTime,
                             @Param("endTime") String endTime);

    //分页查询商品的名称和图片
    @Select("<script>" +
            "select og.* from order_goods as og,order_business as ob  " +
            "where og.OrderBusinessCode = ob.OrderBusinessCode " +
            "and ob.BusinessCode = #{businessCode} " +
            "and ob.CreateTime between #{startTime} and #{endTime} " +
            "and og.number>0 " +
            "GROUP BY og.GoodsCode " +
            "</script>")
    List<GoodsBusinessCodeDTO> getPage(@Param("businessCode")String businessCode,
                                       @Param("startTime") String startTime,
                                       @Param("endTime") String endTime);
}
